package com.bestcem.xm.cjm.controller;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.cjm.service.CjmClientjourneyService;
import com.bestcem.xm.cjm.service.CjmPackageService;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/cjm/")
public class CjmPushController {
    private @NonNull CjmPackageService cjmPackageService;
    private @NonNull CjmClientjourneyService cjmClientjourneyService;

    @ApiOperation(value = "新建旅程")
    @PostMapping("/clientjourneys/")
    public XmResultJson<?> postClientJourneys(@RequestBody JSONObject params) {
        params.put("orgID", SecurityContextHolder.getOrgId());
        params.put("user_id", SecurityContextHolder.getUserId());
        return XmResultJson.success(cjmClientjourneyService.postClientJourneys(params));
    }

    @ApiOperation(value = "删除旅程")
    @DeleteMapping("/clientjourneys/{id:[a-f0-9]{24}}")
    public XmResultJson<?> deleteClientJourneyByid(@PathVariable(value = "id") String id) {
        return XmResultJson.success(cjmClientjourneyService.deleteClientJourneyByid(id));
    }

    @ApiOperation(value = "旅程重命名")
    @PutMapping("/clientjourneys/{id:[a-f0-9]{24}}")
    public XmResultJson<?> putClientJourneys(@PathVariable(value = "id") String id, @RequestBody JSONObject params) {
        return XmResultJson.success(cjmClientjourneyService.putClientJourneys(id, params));
    }

    @ApiOperation(value = "移动旅程")
    @PutMapping("/userclientjourneys/{id:[a-f0-9]{24}}")
    public XmResultJson<?> putUserClientJourneys(@PathVariable(value = "id") String id, @RequestBody JSONObject params) {
        return XmResultJson.success(cjmClientjourneyService.putUserClientJourneys(id, params));
    }

    @ApiOperation(value = "旅程设置顺序")
    @PostMapping("/clientjourney_package_order/")
    public XmResultJson<?> clientJourneyPackageOrder(@RequestBody JSONObject params) {
        return XmResultJson.success(cjmClientjourneyService.clientJourneyPackageOrder(params));
    }

    @ApiOperation(value = "阶段设置")
    @PutMapping("/clientjourney_edit/{id:[a-f0-9]{24}}")
    public XmResultJson<?> putClientJourneyEdit(@PathVariable(value = "id") String id, @RequestBody JSONObject params) {
        return XmResultJson.success(cjmClientjourneyService.putClientJourneyEdit(id, params));
    }

    @ApiOperation(value = "阶段设置获取旅程信息")
    @GetMapping("/clientjourney_edit/{id:[a-f0-9]{24}}")
    public XmResultJson<?> getClientJourneyEdit(@PathVariable(value = "id") String id) {
        return XmResultJson.success(cjmClientjourneyService.getClientJourneyEdit(id));
    }

    @ApiOperation(value = "阶段设置保存")
    @PostMapping("/clientjourney_edit/{id:[a-f0-9]{24}}")
    public XmResultJson<?> postClientJourneyEdit(@PathVariable(value = "id") String id) {
        return XmResultJson.success(cjmClientjourneyService.postClientJourneyEdit(id));
    }

    @ApiOperation(value = "元素设置")
    @PutMapping("/clientjourney_edit_v2/{id:[a-f0-9]{24}}")
    public XmResultJson<?> putClientJourneyEditV2(@PathVariable(value = "id") String id, @RequestBody JSONObject params) {
        return XmResultJson.success(cjmClientjourneyService.putClientJourneyEditV2(id, params));
    }

    /**
     * 用户旅程-图片上传功能
     *
     * @param file 附件
     * @return
     * @author gy.zhang
     * @date 2022/8/10
     */
    @ApiOperation(value = "用户旅程-图片上传功能接口")
    @PostMapping("/clientjourney_image_upload/")
    public XmResultJson<?> clientJourneyImageUpload(@RequestParam("file") MultipartFile file,
                                                    @RequestParam(name = "systemCode") String systemCode,
                                                    @RequestParam(name = "projectID") String projectID,
                                                    @RequestParam(name = "imageScale", required = false) String imageScale,
                                                    @RequestParam(name = "keep_ext", required = false) Boolean keep_ext,
                                                    @RequestParam(name = "image_type_icon", required = false) Boolean image_type_icon) throws Exception {
        if (Validator.isNull(file)) {
            return XmResultJson.fail(4, ":attachment is required");
        }
        if (ObjectUtil.isNotNull(imageScale)) {
            return XmResultJson.fail(4, ":imageScale not support");
        }
        if (!isImage(file)) {
            return XmResultJson.fail(4, ":请上传图片格式:bmp/gif/jpg/png");
        }
        if (ObjectUtil.isNull(keep_ext)) {
            keep_ext = false;
        }
        if (ObjectUtil.isNull(image_type_icon)) {
            image_type_icon = false;
        }


        return XmResultJson.success(cjmClientjourneyService.clientJourneyImageUpload(file, projectID, keep_ext, image_type_icon));
    }

    /**
     * 客户旅程上传图标到企业库
     *
     * @param file 附件
     * @return
     * @author gy.zhang
     * @date 2022/8/10
     */
    @ApiOperation(value = "用户旅程-图片上传功能接口")
    @PostMapping("/clientjourney_imagelib_upload/")
    public XmResultJson<?> clientJourneyImagelibUpload(@RequestParam("file") MultipartFile file,
                                                       @RequestParam(name = "systemCode") String systemCode,
                                                       @RequestParam(name = "projectID") String projectID,
                                                       @RequestParam(name = "imageScale", required = false) String imageScale,
                                                       @RequestParam(name = "keep_ext", required = false) Boolean keep_ext,
                                                       @RequestParam(name = "image_type_icon", required = false) Boolean image_type_icon) throws Exception {
        if (Validator.isNull(file)) {
            return XmResultJson.fail(4, ":attachment is required");
        }
        if (ObjectUtil.isNotNull(imageScale)) {
            return XmResultJson.fail(4, ":imageScale not support");
        }
        if (!isImage(file)) {
            return XmResultJson.fail(4, ":请上传图片格式:bmp/gif/jpg/png");
        }
        if (ObjectUtil.isNull(keep_ext)) {
            keep_ext = false;
        }
        if (ObjectUtil.isNull(image_type_icon)) {
            image_type_icon = false;
        }


        return XmResultJson.success(cjmClientjourneyService.clientJourneyImagelibUpload(file, projectID, keep_ext, image_type_icon));
    }

    @NotNull
    private static JSONObject getFilterFromJwt() {
        JSONObject query_params = new JSONObject();
        query_params.put("orgID", SecurityContextHolder.getOrgId());
        query_params.put("user_id", SecurityContextHolder.getUserId());
        return query_params;
    }

    @NotNull
    private static boolean isImage(MultipartFile file) {
        BufferedImage image = null;
        try {
            image = ImageIO.read(file.getInputStream());
            if (image == null || image.getWidth() <= 0 || image.getHeight() <= 0) {
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}